package com.mths.server.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mths.server.entity.CompanyDocument;
import com.mths.server.entity.DocumentDBStore;

@Repository("companyDocumentDAO")
@Transactional
public class CompanyDocumentDAOImpl extends AbstractDAOImpl<String, CompanyDocument> implements CompanyDocumentDAO {

	@Override
	public List<CompanyDocument> loadCompanyDocument(String selectedCompanyOid) {
		String sqlStr = "select c from CompanyDocument c where c.company.objid = :companyOid ";
		Query query = getEntityManager().createQuery(sqlStr);
		query.setParameter("companyOid", selectedCompanyOid);
		return (List<CompanyDocument>) query.getResultList();
	}

	@Override
	public DocumentDBStore findDocumentDBStoreByOid(String fileDbStoreOid) {
		String sqlStr = "select d from DocumentDBStore d where d.objid = :documentOid ";
		Query query = getEntityManager().createQuery(sqlStr);
		query.setParameter("documentOid", fileDbStoreOid);
		List<DocumentDBStore> result = query.getResultList();
		if (result != null && !result.isEmpty()) {
			return result.get(0);
		}
		return null;
	}
}
